import numpy as np

def signal_delay(
        sig_a: np.ndarray,  # 输入信号
        sig_ref: np.ndarray # 参考信号
        ):
    # 信号延迟（返回以采样点位为单位的延迟）
    # 要求两个信号的序列长度必须一致
    assert sig_a.shape == sig_ref.shape
    N = sig_a.shape[0]
    # 计算互相关函数
    sig_correl = np.correlate(sig_a,sig_ref,mode='full') / 4096.0
    peak_index = sig_correl.argmax() # 计算峰值的索引
    return peak_index - N + 1

def time_delay(
        sig_a: np.ndarray,
        sig_ref: np.ndarray,
        sample_rate: int,
        ):
    assert sig_a.shape == sig_ref.shape
    return signal_delay(sig_a,sig_ref) / sample_rate

if __name__ == '__main__':
    pass